package com.explorati.design_pattern.hybrid.observermediator;

import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

public class EventDispatch implements Observer {

	// 单例模式
	private static EventDispatch dispatcher = new EventDispatch();
	// 事件处理者
	private Vector<EventCustomer> customers = new Vector<EventCustomer>();

	// 不允许生成新的实例
	private EventDispatch() {
	}

	public static EventDispatch getEventDispatch() {
		return dispatcher;
	}

	@Override
	public void update(Observable o, Object arg) {
		Product p = (Product) arg;
		// 事件
		ProductEvent event = (ProductEvent) o;
		// 处理者处理事件，中介者模式核心
		for (EventCustomer e : customers) {
			for (ProductEventType t : e.getCustomerType()) {
				if (t.getValue() == event.getType().getValue()) {
					e.exex(event);
				}
			}
		}
	}

	// 注册事件处理者
	public void registerCustomer(EventCustomer customer) {
		customers.addElement(customer);
	}
}
